//
//  JWBrowseViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/21.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWBrowseViewController.h"
#import "JWNewsPictureModel.h"
#import "UIImageView+WebCache.h"
#import "constant.h"

@interface JWBrowseViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageCtrl;
@property (nonatomic, strong) UIImageView *chengedImg;
@property (nonatomic, strong) UIScrollView *chengedScroll;
@property (nonatomic, assign) CGPoint imgCenter;
@end

@implementation JWBrowseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self addScrollView];
    [self addImages];
    [self addItems];
}
- (void)addItems{
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 50, 30)];
    [cancleBtn addTarget:self action:@selector(cancleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.view addSubview:cancleBtn];
}

- (void)cancleBtnAction:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, JWScreenWidth ,JWScreenHeight - 64)];
    
    _scrollView.contentSize = CGSizeMake(JWScreenWidth * _imageAr.count, JWScreenHeight - 64);
    _scrollView.contentOffset = CGPointMake(JWScreenWidth * _clickTag, 0);//设置初始显示的图片
    
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_scrollView];
    _scrollView.delegate = self;
}
- (void)addImages{
    
    for (int i = 0; i < _imageAr.count; i++) {
        UIScrollView *boreView = [[UIScrollView alloc] initWithFrame:CGRectMake(JWScreenWidth * i, 0, JWScreenWidth, JWScreenHeight - 64)];
        boreView.backgroundColor = [UIColor blackColor];
        boreView.contentSize = CGSizeMake(JWScreenWidth, JWScreenHeight - 64);
        boreView.contentOffset = CGPointMake(0, 0);
        boreView.tag = i;
        boreView.showsVerticalScrollIndicator = NO;
        boreView.showsHorizontalScrollIndicator = NO;
        boreView.scrollEnabled = NO;
        
        //缩放比例控制
        boreView.minimumZoomScale = 0.5;
        boreView.maximumZoomScale = 5;
        boreView.delegate = self;
        
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 300)];
        imgView.center = CGPointMake(JWScreenWidth * 0.5, boreView.center.y);
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        JWNewsPictureModel *model = self.imageAr[i];
        NSString *urlStr = model.image;
        
        [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"sctpxw@2x"]];
        
        [boreView addSubview:imgView];
        
        [_scrollView addSubview:boreView];
    }
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    NSInteger CurrentTag = _scrollView.contentOffset.x / JWScreenWidth;
    _chengedScroll = (UIScrollView *)[scrollView viewWithTag:CurrentTag];
    _chengedImg = [scrollView viewWithTag:CurrentTag].subviews[0];
    
    return _chengedImg;//变动的图片
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    _imgCenter = _chengedImg.center;//保存中心点
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    _chengedImg.center = _imgCenter;//保持中心点不变
    
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    if (scrollView == _scrollView) {
        [_chengedScroll setZoomScale:1];//将改变尺寸的图片还原尺寸
        
    }
}


@end
